package ecom.beans.dao;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.Set;
import org.hibernate.Query;

public abstract class DAO {
    
    private static Session session;
    private static Transaction tx;
    
    protected static void catchException(HibernateException e) {
        HibernateUtil.rollback(tx);
        System.out.println("Rollback transaction" + e);
    }
    
    protected static void begin() throws HibernateException {
        session = HibernateUtil.openSession();
        tx = session.beginTransaction();
    }
    
    protected static boolean saveOrUpdate(Object obj) {
        boolean isGood = false;
        try {
            begin();
            session.saveOrUpdate(obj);
            tx.commit();
            isGood = true;
        } catch (HibernateException e) {
            catchException(e);
            System.err.println("SaveOrUpdate fail" + e);
        } finally {
            HibernateUtil.close(session);
            return isGood;
        }
    }
    
    protected static boolean delete(Object obj) {
        boolean isGood = false;
        try {
            begin();
            session.delete(obj);
            tx.commit();
            isGood = true;
        } catch (HibernateException e) {
            catchException(e);
            System.err.println("Delete fail" + e);
        } finally {
            HibernateUtil.close(session);
            return isGood;
        }
    }
    
    protected static void saveOrUpdateAll(List obj) {
        try {
            begin();
            Iterator it = obj.iterator();
            while (it.hasNext()) {
                session.saveOrUpdate(it.next());
            }
            tx.commit();
        } catch (HibernateException e) {
            catchException(e);
            System.err.println("SaveOrUpdateAll fail" + e);
        } finally {
            HibernateUtil.close(session);
        }
    }
    
    protected static void deleteAll(List obj) {
        try {
            begin();
            Iterator it = obj.iterator();
            while (it.hasNext()) {
                session.delete(it.next());
            }
            tx.commit();
        } catch (HibernateException e) {
            catchException(e);
            System.err.println("deleteAll fail" + e);
        } finally {
            HibernateUtil.close(session);
        }
    }
    
    protected static Object fetchOne(String nomRequete, List parameters) {
        Object obj = null;
        try {
            begin();
            Query query = session.getNamedQuery(nomRequete);
            if (parameters != null) {
                Iterator it = parameters.iterator();
                while (it.hasNext()) {
                    query.setParameter((String) it.next(), it.next());
                }
            }
            if (!query.list().isEmpty()) {
                obj = query.uniqueResult();
            }
            tx.commit();
        } catch (HibernateException e) {
            catchException(e);
            System.err.println("Method fetchOne fail" + e);
        } finally {
            HibernateUtil.close(session);
        }
        return obj;
    }
    
    protected static Set fetchAll(String nomRequete, List parameters, String order) {
        Set objectsSet = null;
        List objectsList = null;
        String sql = null;
        Query query = null;
        try {
            begin();
            if (order != null) {
                sql = session.getNamedQuery(nomRequete).getQueryString() + order;
                query = session.createQuery(sql);
            } else {
                query = session.getNamedQuery(nomRequete);
            }
            
            if (parameters != null) {
                Iterator it = parameters.iterator();
                while (it.hasNext()) {
                    query.setParameter((String) it.next(), it.next());
                }
            }
            objectsList = query.list();
            objectsSet = new LinkedHashSet(objectsList);
            tx.commit();
        } catch (HibernateException e) {
            catchException(e);
            System.err.println("Method fetchAll fail" + e);
        } finally {
            HibernateUtil.close(session);
        }
        return objectsSet;
    }
    
    protected static Set fetchAllLimit(String nomRequete, List parameters, String order, Integer limit) {
        Set objectsSet = null;
        List objectsList = null;
        String sql = null;
        Query query = null;
        try {
            begin();
            if (order != null) {
                sql = session.getNamedQuery(nomRequete).getQueryString() + order;
                query = session.createQuery(sql);
            } else {
                query = session.getNamedQuery(nomRequete);
            }
            if (limit != null) {
                query.setMaxResults(limit);
            }
            if (parameters != null) {
                Iterator it = parameters.iterator();
                while (it.hasNext()) {
                    query.setParameter((String) it.next(), it.next());
                }
            }
            objectsList = query.list();
            objectsSet = new LinkedHashSet(objectsList);
            tx.commit();
        } catch (HibernateException e) {
            catchException(e);
            System.err.println("Method fetchAll fail" + e);
        } finally {
            HibernateUtil.close(session);
        }
        return objectsSet;
    }
}
